<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">
  <div style="height: 200px">
    <a-date-picker placeholder="请选择数据日期"  @change="onChange"  v-model="sjrq" :default-value=getCurrentData() :format="dateFormat"  format='YYYY-MM-DD' style="width: 220px" />
  </div>

  </j-modal>
</template>

<script>
  import { judgeSjrq } from '@/utils/util'
  import { httpAction } from '@/api/manage'
  import { downFile } from '@/api/manage'
  import moment from 'moment'

  export default {
    name: 'ExportModal',
    components: {
    },
    data () {
      return {
        model:{
        },
        fileName: '',
        sjrq:'',
        params:{},
        title:'导出',
        width:900,
        visible: false,
        disableSubmit: false
      }
    },
    props: {
      handleAdd: {
        type: Function,
        default: null
      }
    },
    methods: {
      getCurrentData(){
        return moment(new Date().toLocaleDateString(), this.dateFormat)
      },
      onChange(date, dateString) {
        this.sjrq = dateString
      },
      showModal(fileName,params) {
        //用于弹出框类型
        var str=fileName.split(",");
        this.fileName  = str[0];
        if(str.length>1){
          this.params.type=str[1];
        }else {
          this.params.type='0';
        }
        //this.params  = params;
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk(e) {
        let rq = moment(new Date().toLocaleDateString()).format("YYYY-MM-DD");
        if(this.sjrq == ''){
          this.sjrq = rq
        }
        if(this.sjrq == undefined || this.sjrq == ''){
          this.$message.warning("请选择数据日期")
          return;
        }
        if(!this.fileName || typeof this.fileName != "string"){
          this.fileName = "导出文件"
        }
        let downloadUrl = this.$parent.$parent.url.exportXlsUrl;
        this.params.sjrq = this.sjrq;
        let param = this.params;
        downFile(downloadUrl,param).then((data)=>{
          if (!data) {
            this.$message.warning("文件下载失败")
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), this.fileName+'.xlsx')
          }else{
            let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', this.fileName+'.xlsx')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
            //关闭窗口
            this.handleCancel();
          }
        });

      },
      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel () {
        this.sjrq = '';
        this.close()
      },
    }
  }
</script>